package com.easy.controller;

import com.easy.bean.Course;
import com.easy.common.CommonResult;
import com.easy.service.ICourseService;
import com.easy.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class CourseController {

    @Autowired
    ICourseService courseS;

    @GetMapping("private")
    public CommonResult getPrivateCourse(Course course, Page page){
        List<Course> list = courseS.getPrivateCourse(course,page);
        int count = courseS.getPrivateCourseCount(course);
        HashMap map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return CommonResult.success(map);
    }

    @PostMapping("private")
    public CommonResult addPrivateCourse(@RequestBody Course course){
        courseS.addPrivateCourse(course);
        course = courseS.getCourseByID(course.getId());
        return CommonResult.success(course);
    }

    @DeleteMapping("course/{id}")
    public CommonResult delCourseByID(@PathVariable int id){
        int res = courseS.delCourseByID(id);
        return CommonResult.success(res);
    }

    @PutMapping("private")
    public CommonResult editPrivateCourse(@RequestBody Course course){
        courseS.editPrivateCourse(course);
        course = courseS.getCourseByID(course.getId());
        return CommonResult.success(course);
    }

    @GetMapping("public")
    public CommonResult getPublicCourse(Course course, Page page){
        List<Course> list = courseS.getPublicCourse(course,page);
        int count = courseS.getPublicCourseCount(course);
        HashMap map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return CommonResult.success(map);
    }

    @PostMapping("public")
    public CommonResult addPublicCourse(@RequestBody Course course){
        courseS.addPublicCourse(course);
        course = courseS.getCourseByID(course.getId());
        return CommonResult.success(course);
    }

    @PutMapping("public")
    public CommonResult editPublicCourse(@RequestBody Course course){
        courseS.editPublicCourse(course);
        course = courseS.getCourseByID(course.getId());
        return CommonResult.success(course);
    }

}
